package io.gitee.hchaojie.day08.thread;

public class TestPrint {
    public static void main(String[] args) {
        Thread thread1 = new PrintString();
        Thread thread2 = new PrintNumber(10);
        thread1.start();
        thread2.start();

        try {
            thread1.join();     // 让当前线程进入等待状态，等待join的那个线程执行完毕
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("主线程执行完毕。。");
    }
}

// run()方法里面的代码执行完毕，线程被销毁
class PrintString extends Thread {
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("字符串打印线程。。。");
        }
    }
}

class PrintNumber extends Thread {
    private int num;
    public PrintNumber(int num) {
        this.num = num;
    }

    @Override
    public void run() {
        while (this.num > 0) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(this.num);
            this.num --;
        }
    }
}
